//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;
using JetBrains.Annotations;
using LargoCommon.Abstract;
using LargoCommon.Music;
namespace LargoCommon.Support
{
///
/// MusicXml Header.
///
public sealed class MusicXmlHeader
{
#region Fields
///
/// Musical File.
///
private MusicalBlock musicalBlock;
#endregion
#region Constructors
///
/// Initializes a new instance of the MusicXmlHeader class.
///
/// Score Part wise.
/// Musical block.
public MusicXmlHeader(XElement givenScorePartwise, MusicalBlock givenMusicalBlock) {
this.ScorePartwise = givenScorePartwise;
this.MusicalBlock = givenMusicalBlock;
}
///
/// Initializes a new instance of the class.
///
[UsedImplicitly]
public MusicXmlHeader() {
}
#endregion
#region Public Properties
///
/// Gets Score Part wise.
///
/// Property description.
public XElement ScorePartwise { get; }
///
/// Gets Score Parts.
///
/// Property description.
public Dictionary ScoreParts { get; private set; }
#endregion
#region Private Properties
///
/// Gets or sets Musical File.
///
/// Property description.
private MusicalBlock MusicalBlock {
get {
Contract.Ensures(Contract.Result() != null);
if (this.musicalBlock == null) {
throw new InvalidOperationException("Musical block is null.");
}
return this.musicalBlock;
}
set => this.musicalBlock = value;
}
#endregion
#region Private static methods
///
/// Get Note Type.
///
/// Length quotient.
/// Returns value.
public static string GetNoteType(float quotient) { //// whole, half, quarter, eighth, sixteenth
string noteType;
const float delta = 0.01f;
if (quotient >= DefaultValue.Unit - delta) {
noteType = "whole";
}
else {
if (quotient >= DefaultValue.HalfUnit - delta) {
noteType = "half";
}
else {
noteType = quotient >= DefaultValue.QuarterUnit - delta ? "quarter"
: quotient >= DefaultValue.EightUnit - delta ? "eighth" : "sixteenth";
}
}
return noteType;
}
#endregion
#region Operations
///
/// Read Xml Header.
///
public void ReadXmlHeader() {
Contract.Requires(this.ScorePartwise != null);
var work = this.ScorePartwise.Element("work");
this.ReadWorkElement(work);
//// string movementNumber = (string)this.ScorePartwise.Element("movement-number");
//// string movementTitle = (string)this.ScorePartwise.Element("movement-title");
var identification = this.ScorePartwise.Element("identification");
this.ReadIdentificationElement(identification);
var partList = this.ScorePartwise.Element("part-list");
if (partList != null) {
this.ReadScorePartListElement(partList);
}
}
///
/// Write Xml Header.
///
public void WriteXmlHeader() {
var element = this.WorkElement();
this.ScorePartwise.Add(element);
var movementNumber = new XElement("movement-number", string.Empty);
this.ScorePartwise.Add(movementNumber);
var movementTitle = new XElement("movement-title", "movement");
this.ScorePartwise.Add(movementTitle);
element = this.IdentificationElement();
this.ScorePartwise.Add(element);
}
#endregion
#region Private methods
///
/// Read ScorePartList Element.
///
/// Score PartList.
private void ReadScorePartListElement(XContainer partList) {
Contract.Requires(partList != null);
this.ScoreParts = new Dictionary();
foreach (var scorePartObject in
partList.Elements("score-part").Select(ScorePartObject.ReadScorePart)) {
this.ScoreParts[scorePartObject.Id] = scorePartObject;
}
}
///
/// Work element.
///
/// Musical work.
private void ReadWorkElement(XContainer work) {
if (work == null) {
return;
}
this.MusicalBlock.FileHeading = new FileHeading {
WorkNumber = (string)work.Element("work-number"),
WorkTitle = (string)work.Element("work-title")
};
//// this.MusicalBlock.Name = this.MusicalBlock.FileHeading.WorkTitle;
}
///
/// Work element.
///
/// Returns value.
private XElement WorkElement() {
var work = new XElement(
"work",
new XElement("work-number", this.MusicalBlock.FileHeading.WorkNumber ?? string.Empty),
new XElement("work-title", this.MusicalBlock.FileHeading.WorkTitle ?? this.MusicalBlock.Header.Name));
return work;
}
///
/// Identification element.
///
/// Musical encoding.
private void ReadEncodingElement(XContainer encoding) {
if (encoding == null) {
return;
}
//// this.MusicalBlock.Name = (string)work.Attribute("work-number");
this.MusicalBlock.FileHeading = new FileHeading {
EncodingDate = (string)encoding.Element("encoding-date"),
Encoder = (string)encoding.Element("encoder"),
Software = (string)encoding.Element("software"),
EncodingDescription = (string)encoding.Element("encoding-description")
};
//// DateTime
}
///
/// Identification element.
///
/// Returns value.
private XElement EncodingElement() {
//// "2011-01-01";
var encoding = new XElement(
"encoding",
new XElement("encoding-date", this.MusicalBlock.FileHeading.EncodingDate),
new XElement("encoder", this.MusicalBlock.FileHeading.Encoder),
new XElement("software", this.MusicalBlock.FileHeading.Software),
new XElement("encoding-description", this.MusicalBlock.FileHeading.EncodingDescription));
return encoding;
}
///
/// Identification element.
///
/// Musical identification.
private void ReadIdentificationElement(XContainer identification) {
if (identification == null) {
return;
}
var encoding = identification.Element("encoding");
this.ReadEncodingElement(encoding);
//// this.MusicalBlock.Name = (string)work.Attribute("work-number");
//// string creator = (string)encoding.Element("creator");
//// string composer = (string)encoding.Element("composer");
//// string rights = (string)encoding.Element("rights");
//// string source = (string)encoding.Element("source");
}
///
/// Identification element.
///
/// Returns value.
private XElement IdentificationElement() {
//// "Largo-muse";
//// "Largo-muse";
var encoding = this.EncodingElement();
var identification = new XElement("identification");
if (!string.IsNullOrEmpty(this.MusicalBlock.FileHeading.Creator)) {
identification.Add(new XElement("creator", new XAttribute("type", "poet"), this.MusicalBlock.FileHeading.Creator));
}
if (!string.IsNullOrEmpty(this.MusicalBlock.FileHeading.Composer)) {
identification.Add(new XElement("composer", new XAttribute("type", "poet"), this.MusicalBlock.FileHeading.Composer));
}
if (!string.IsNullOrEmpty(this.MusicalBlock.FileHeading.Rights)) {
identification.Add(new XElement("rights", this.MusicalBlock.FileHeading.Rights));
}
identification.Add(encoding);
if (!string.IsNullOrEmpty(this.MusicalBlock.FileHeading.Source)) {
identification.Add(new XElement("source", this.MusicalBlock.FileHeading.Source));
}
return identification;
}
#endregion
}
}